package com.qxgl.qxglt.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.qxgl.qxglt.dao.IUsersDao;
import com.qxgl.qxglt.dto.UsersDTO;
import com.qxgl.qxglt.model.Roles;
import com.qxgl.qxglt.model.Users;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;


@Service
public class UsersServiceImpl implements IUsersService {

   
	@Resource
    private IUsersDao usersDao;

    @Override
    public Page<Users> findPage(UsersDTO dto) {
        Sort sort = Sort.by(Sort.Order.asc("id"));
        Pageable page = PageRequest.of((dto.getPage() - 1) * dto.getLimit(), dto.getLimit(), sort);
        return usersDao.findAll(getWhereClause(dto), page);
    }

    private Specification<Users> getWhereClause(UsersDTO dto) {
        return new Specification<Users>() {
            private static final long serialVersionUID = 1L;
            @Override
            public Predicate toPredicate(Root<Users> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                Predicate p = criteriaBuilder.conjunction();
                List<Expression<Boolean>> exlList = p.getExpressions();
                if (dto.getLoginName() != null && "".equals(dto.getLoginName())) {
                    exlList.add(criteriaBuilder.equal(root.<String>get("loginName"), "%" + dto.getLoginName() + "%"));
                }
                if (dto.getStartcreateTime() != null) {
                    exlList.add(criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("createTime"),
                            dto.getStartcreateTime()));
                }
                if (dto.getEndcreateTime() != null) {
                    exlList.add(
                            criteriaBuilder.lessThanOrEqualTo(root.<Date>get("createTime"), dto.getEndcreateTime()));
                }
                if (dto.getIsLockout() != null && "".equals(dto.getIsLockout())) {
                    exlList.add(criteriaBuilder.equal(root.<String>get("isLockout"), dto.getIsLockout()));
                }
                return p;
            }

        };
    }

    @Override
    public Users saveUsers(Users u) {    
        return usersDao.save(u);
    }
    @Override
    public void setRoles(String userId, List<String> roleIdList) {
        Users user = usersDao.getOne(userId);
        for (String roleId : roleIdList) {
            Roles role=new Roles();
            role.setId(roleId);
            user.getRolesList().add(role);
        }
        usersDao.save(user);

    } 

    @Override
    public List<Users> findAll() {
        return usersDao.findAll();
    }





    @Override
    public void del(String id) {
       usersDao.deleteById(id);

    }

}